package com.jacky.concurrent;

import com.sun.org.apache.xpath.internal.SourceTree;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * Created by jacky on 2018/2/11.
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) throws InterruptedException {
        MyCyclicBarrier barrier = new MyCyclicBarrier(3, new Runnable() {
            @Override
            public void run() {
                Thread thread = Thread.currentThread();
                System.out.println("barrierAction start"+thread.getName());
                try {
                    Thread.sleep((int)Math.random()*300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("barrierAction start"+thread.getName());
            }
        });
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep((int)(Math.random()*100));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread thread = Thread.currentThread();
                System.out.println("thread start:"+thread.getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("thread end:"+thread.getName());
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep((int)(Math.random()*200));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread thread = Thread.currentThread();
                System.out.println("thread start:"+thread.getName());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("thread end:"+thread.getName());
            }
        };
        Thread thread1 = new Thread(runnable1);
        Thread thread2 = new Thread(runnable2);
        Thread thread3 = new Thread(runnable1);
        thread1.start();
        thread2.start();
        thread3.start();
        Thread.sleep(2000);
        System.out.println("thread1:"+thread1.getName()+","+thread1.getState());
        System.out.println("thread2:"+thread2.getName()+","+thread2.getState());
        System.out.println("thread3:"+thread3.getName()+","+thread3.getState());
    }
}
